﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Model;
using OA.Model.DB;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;


namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class ProductCategoryController : BaseMemberController
    {

        private IMemoryCache _cache;
        public ProductCategoryController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("207002", "产品类别", "单位与产品", true)]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilter("207002", "产品类别")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<ProductCategory, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Name.Contains(keyword));
            }
            PageList<ProductCategory> list = this.GetPageList<ProductCategory>(where, page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilter("207002001", "添加类别", "产品类别", false, "")]
        public IActionResult Create()
        {
            return View(new ProductCategory());
        }




        [HttpPost]
        [MyActionFilter("207002001", "添加类别", "产品类别", false, isUpdateDataBase: true)]
        public IActionResult Create(ProductCategory model, List<int> Department)
        {
            Department = Department.Where(m => m > 0).ToList();
            model.CreateDateTime = DateTime.Now;
            BaseResult baseResult = new BaseResult();

            if (Department == null|| Department.Count==0)
            {
                model.ParentID = 0;
            }
            else
            {
                model.ParentID = Department.Last() ;
            }

            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);                 
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<ProductCategory>(model);
                baseResult.Msg = "产品类别添加成功";
                return Json(baseResult);

            }

        }

        [HttpGet]
        [MyActionFilter("207002002", "编辑类别", "产品类别", false, "")]
        public IActionResult Edit(int id)
        {
            ProductCategory infoProductCategory = this.GetInfo<ProductCategory>(id);
            if (infoProductCategory == null)
            {
                return Redirect("/Admin/Home/ShowInfo?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该产品类别", Encoding.UTF8));
            }
            return View(infoProductCategory);
        }

        [HttpPost]
        [MyActionFilter("207002002", "编辑类别", "产品类别", false,isUpdateDataBase:true)]
        public IActionResult Edit(int id, ProductCategory model, List<int> Department)
        {
            Department = Department.Where(m => m > 0).ToList();
            model.CreateDateTime = DateTime.Now;
            BaseResult baseResult = new BaseResult();

            if (Department == null || Department.Count == 0)
            {
                model.ParentID = 0;
            }
            else
            {
                model.ParentID = Department.Last();
            }

            int parentID = model.ParentID;

            if(parentID>0)
            {
                if (parentID == model.ID)
                {
                    baseResult.State = 400;
                    baseResult.Msg = "不能将自己作为自己的父级";
                    return Json(baseResult);
                }

                //不能将自己的下级作为自己的父级
                var list = this.dbContextFactory.DbContext.ProductCategory.ToList();
                List<int> children = new List<int>();

                children.Add(model.ID);

                while (true)
                {
                    var items = list.Where(m => children.Contains(m.ParentID)).Select(m => m.ID).ToList();
                    if (items.Count == 0) break;
                    if (items.Contains(parentID))
                    {
                        baseResult.State = 400;
                        baseResult.Msg = "不能将自己的下级作为自己的父级";
                        return Json(baseResult);
                    }
                    children = items;
                }
            }


            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            ProductCategory infoPageGroup = this.GetInfo<ProductCategory>(id);
            if (infoPageGroup == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该产品类别";
                return Json(baseResult);
                 
            }
            this.Update<ProductCategory>(infoPageGroup, model, new string[] { "Name", "Sort", "ParentID" });
            baseResult.Msg = "产品类别修改成功";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilter("207002003", "删除类别", "产品类别", false, "")]
        public IActionResult Delete(long id)
        {
            Expression<Func<ProductCategory, bool>> where = m => m.ID == id;
            this.Delete<ProductCategory>(where);
            return Json(new { State = 200, Msg = "删除成功" });
        }
    }
}
